﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class AccessoryStockInList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
            }
        }
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = "";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and (supplyno like '%" + txtContractCodeBasic.Text.Trim() + "%' or BillCode like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (txtAccessoryCompanyNum.Text.Trim() != "")
            {
                Whereclause += " and StockInID in (select StockInID from Cms_Wsm_AccessoryStock where AccessoryCompanyNum = '" + txtAccessoryCompanyNum.Text.Trim() + "')";
            }

            if (txtAccessoryCode.Text.Trim() != "")
            {
                Whereclause += " and StockInID in (select StockInID from Cms_Wsm_AccessoryStock where AccessoryCode = '" + txtAccessoryCode.Text.Trim() + "')";
            }


            int RecordCount = 0;
            List<WsmAccessoryStockInEntity> list = new SysUserBll().GetAll<WsmAccessoryStockInEntity>(Whereclause, "CreatedOn desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = list;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        private void BindList1(int pageindex)
        {
            int RecordCount = 0;

            string whereSql = " and StockInID='" + hiddenStockID.Value + "'";
            DataTable dt = new StockBll().QueryAccessoryStock(whereSql, "AccessoryCode asc", AspNetPager2.PageSize, pageindex, out RecordCount);

            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager2.RecordCount = RecordCount;
        }
        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "delete")
            {
                Label StockInID = e.Item.FindControl("StockInID") as Label;              
                List<WsmAccessoryStockEntity> listCheck = new SystemBll().GetAll<WsmAccessoryStockEntity>(" and StockInID = '" + StockInID.Text + "' and (AccessoryStatus <> 0)");
                if (listCheck != null && listCheck.Count > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "alert('单据中的配件已在出库状态，不能删除！')", true);
                    return;
                }
                new SystemBll().Delete<WsmAccessoryStockEntity>(" and StockInID = '" + StockInID.Text + "'");
                new SystemBll().Delete<WsmAccessoryStockInEntity>(" and StockInID = '" + StockInID.Text + "'");
                BindList(1);
            }

            if (e.CommandName == "view")
            {
                Label StockInID = e.Item.FindControl("StockInID") as Label;
                hiddenStockID.Value = StockInID.Text;
                
                BindList1(1);
            }

        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindList1(AspNetPager2.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblOperatorValue = e.Item.FindControl("lblOperatorValue") as Label;
                Label lblOperator = e.Item.FindControl("lblOperator") as Label;
                LinkButton ltnDel = e.Item.FindControl("ltnDel") as LinkButton;

                if (lblOperator != null && lblOperatorValue != null)
                {
                    SysUserEntity entity = new SysUserBll().Get<SysUserEntity>(new Guid(lblOperatorValue.Text));
                    lblOperator.Text = entity.UserName;
                }

                ltnDel.Visible = false;
                ltnDel.OnClientClick = "return OnDelConfirm();";

                if (base.CheckUserHasFuction(1, "Func6012"))
                { ltnDel.Visible = true; }
            }
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = " and StockInID='"+hiddenStockID.Value+"'";

            DataTable dt = new StockBll().ExportAccessoryStockList(Whereclause, " AccessoryCode");

            Export.Export2Excel(dt, "入库明细表", Page.Response);

        }
    }
}
